﻿package com.ourbrander.webObj.loader{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.ProgressEvent
	import com.ourbrander.Event.superEvent;
	import com.ourbrander.xmlObject.xmlFrame;
	import flash.events.EventDispatcher
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import com.ourbrander.Event.superEvent
	public class preLoader {
		public static  var _singleton:Boolean;//是否是单例
		public static const  COMPELETE = "compelete"
		public static const  PUASE = "puase"
		public static const  STOP = "stop"
		public static const  USERLOADING = "userLoading"
		public static const  USERCOMPELETE = "userCompelete"
		
		private var _source:xmlFrame;//预加载的xml化序列对象
		public  var _eventDispacher:EventDispatcher //发出和接受各种事件
		private var _index:uint//下载索引
		private var _load:Loader//下载容器
		private var _count:uint//下载文件总数
		public function preLoader() {
			if (_singleton!=true) {
				init();
			} else {

				throw new Error("本类是单例类，全局类只能有一个");

			}//end if
		}//end function
		private function init() {
			_singleton = true;
			_index = 0
			_eventDispacher=new EventDispatcher()
			_source = new xmlFrame()
			_load=new Loader()
			_load.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded)
			_load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,puaseLoad)
			//_load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loading)
		}
		//设定预加载的列表XML文件
		public function set source(xml:*) {
			if (xml is XML) {
				_source.addEventListener(Event.COMPLETE,sourceLoaded);
				_source.xml = xml;

			} else {
				
				_source.addEventListener(Event.COMPLETE, sourceLoaded);

				_source.loadXML(xml);
			}
		}
		
		public function get source() {
			
             switch(_source.xmlState){
				case "init":
				//trace("还没有设置预加载的数据源")
				return _source
				//throw new Error("还没有设置预加载的数据源");
				break;
				case "loading":
				//trace("XML数据还未加载完")
				return _source
				break;
				case "compelete":
				return  _source
				break;
			
			 }
			 
		}//end function
		
		private function sourceLoaded(e :Event= null) {
			//当预加载xml数据清单下载完毕
			//trace("当预加载xml数据清单下载完毕")
			 _count = source.xml.child("*").length()
			 trace(_count)
			 //startLoad(e)
		}
		
		//开始下载
		private function startLoad(e = null) {
			
		 load(e)
		}
		//暂停下载
		private function puaseLoad(e = null) {
			trace("暂停预加载")
		_load.close()
		}
		//停止下载
		private function stopLoad(e=null) {
		
		}
		//获取和设置下载索引
		private function set index(num:uint) {
			_index=num
		}
		private function get index() {
			 
			return _index 
		}
		//下载
		private function load(e = null) {
			
			
			   index++
			   var path = source.xml.child(index-1).@path
			   _load.load(new URLRequest(path))
			   
		}
		//清单文件正在下载中
		private function loading(e:ProgressEvent = null) {
		trace("文件" + index + "：正在下载>" + e.bytesLoaded + "/" + e.bytesTotal)
		trace(e.target)
		}
		private function loaded(e:Event = null) {
			trace("文件" + index + "：下载完毕")
			if (index <=_count-1) {
			 load()
			}else {
			 trace("下载完毕")
			 var event = new superEvent(preLoader.COMPELETE, true, false)
			 source.dispatchEvent(event)
			}
		}//end function
		//对需要下载的xml对象进行排序，确定下载顺序。
		public function order() {
		
		}
	}
}